//
//  LoginViewController.m
//  InviteJob
//
//  Created by LangLang on 17/10/31.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "ForgetPasswordViewController.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *txtMoblie;
@property (weak, nonatomic) IBOutlet UITextField *txtPasswd;
@property (weak, nonatomic) IBOutlet UIView *bgViewHeight;


@end

@implementation LoginViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    TitleView(@"登录")
    
//    self.navigationController.navigationBar.backgroundColor = RGBA(1, 183, 283, 1);
    
    _txtPasswd.secureTextEntry = YES;
    
    if (_strIndex) {
        [_bgViewHeight mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@64);
            make.left.right.bottom.equalTo(@0);
        }];
    }
    
}


//登录
- (IBAction)btnLoginIn:(id)sender {
    
    if ([SHJudgeTools valiMobile:_txtMoblie.text]) {
        SHuiMBhudTextTimeStr(@"请输入手机号");
        return;
    }
    if (![SHJudgeTools validatePassword:_txtPasswd.text]) {
        SHuiMBhudTextTimeStr(@"请输入密码");
        return;
    }
    NSString *strUrl = [NSString stringWithFormat:user_login_api];
    
    NSDictionary *parameter = @{@"phone":_txtMoblie.text,
                                @"password":_txtPasswd.text};
    
    [MBProgressHUD showMessag:@"登录中..." withDelay:0 toView:self. view withMode:MBProgressHUDModeIndeterminate hideWithDelay:0];
    [PostNetWorking postNetWorkingStr:strUrl andparameters:parameter Success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"code"] intValue] == 200) {
            // 获取数据
            [self getDataId:responseObject[@"datas"][@"user_id"] andToken:responseObject[@"datas"][@"token"]];
            
            SHuiMBHidleYes
            SHuiMBhudTextTimeStr(@"登录成功");
            
            // 绑定极光推送别名
            [[NSNotificationCenter defaultCenter] postNotificationName:@"bindAlia" object:@"YES"];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
                
            });
            
            
        }else{
            SHuiMBHidleYes
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes
        SHuiMBhudTextTimeStr(@"网络错误");
    }];
    
}


//注册
- (IBAction)btnRegist:(id)sender {
    RegistViewController *regist = [RegistViewController new];
    [self.navigationController pushViewController:regist animated:YES];
}


//忘记密码
- (IBAction)btnForget:(id)sender {
    ForgetPasswordViewController *forget = [ForgetPasswordViewController new];
    [self.navigationController pushViewController:forget animated:YES];
}

#pragma mark - 获取数据
- (void)getDataId:(NSString*)userId andToken:(NSString *)token{
    NSString *strUrl = [NSString stringWithFormat:get_UserById_api];
    NSDictionary *parameter = @{@"user_id":userId, @"token":token};
    [PostNetWorking postNetWorkingStr:strUrl andparameters:parameter Success:^(id responseObject) {
        int code = [responseObject[@"code"]intValue];
        switch (code) {
            case 200:
            {
                // 存储用户信息
                [UserModel responseObject:responseObject];
                
            }
                break;
            case 210:
            {
                //逻辑错误
                [MBProgressHUD showMessag:responseObject[@"mark"] withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
            }
                break;
            case 220:
            {
                //token过期
                [MBProgressHUD showMessag:@"登录状态已过期，请重新登录" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
            }
            default:
                break;
        }
        
    } failed:^(NSError *error) {
        [MBProgressHUD showMessag:@"网络错误" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
    }];
}

 


@end
